﻿using MetroDataApi.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace MetroDataApi.Controllers.Management
{
    public class DeletePointByTimeController : ApiController
    {


        // DELETE api/deletemonitorrecord/5
        public string Delete(int prjId, string date, string monitorType = "", string location = "")
        {
            using (var db = new MetroDataDataContext())
            {
                if (location == null || location == "")
                {
                    monitorType = "";
                    location = "";
                }
                var dateTime = DateTime.Parse(date);
                var ptNames = new List<string>();
                if (monitorType == "")
                {
                    ptNames = db.MonitorPointsDictionary.Where(p => p.PrjId == prjId).Select(p => p.Name).ToList();
                }
                else if (monitorType != "" && location == "")
                {
                    ptNames = db.MonitorPointsDictionary.Where(p => p.PrjId == prjId &&
                         p.MonitorType == monitorType).Select(p => p.Name).ToList();
                }
                else
                {
                    ptNames = db.MonitorPointsDictionary.Where(p => p.PrjId == prjId && p.Location == location &&
                        p.MonitorType == monitorType).Select(p => p.Name).ToList();
                }

                var points = db.MonitorPoints.Where(p => p.PrjId == prjId && p.DateTime.Value == dateTime);

                var pts = (from p in ptNames
                           from q in points
                           where q.Name == p
                           select q).ToList();


                db.MonitorPoints.DeleteAllOnSubmit(pts);
                db.SubmitChanges();
                return "OK";
            }
        }
    }
}
